package report

import (
	"fmt"

	"github.com/shirou/gopsutil/v3/load"
	"gopkg.in/yaml.v2"
)

type LoadInfo struct {
	Avg1  float64 `json:"avg1" yaml:"avg1"`
	Avg5  float64 `json:"avg5" yaml:"avg5"`
	Avg15 float64 `json:"avg15" yaml:"avg15"`

	ProcsTotal   int `json:"procsTotal" yaml:"procsTotal"`
	ProcsCreated int `json:"procsCreated" yaml:"procsCreated"`
	ProcsRunning int `json:"procsRunning" yaml:"procsRunning"`
	ProcsBlocked int `json:"procsBlocked" yaml:"procsBlocked"`
	Ctxt         int `json:"ctxt" yaml:"ctxt"`
}

func (l LoadInfo) Show() {
	out, err := yaml.Marshal(l)
	if err != nil {
		return
	}

	fmt.Printf("load : %v\n", string(out))

}

func FetchLoadInfo() LoadInfo {
	var info LoadInfo

	avgStat, err := load.Avg()
	if err != nil {
		return info
	}

	miscStat, err2 := load.Misc()
	if err2 != nil {
		return info
	}

	info = LoadInfo{
		Avg1:         avgStat.Load1,
		Avg5:         avgStat.Load5,
		Avg15:        avgStat.Load15,
		ProcsTotal:   miscStat.ProcsTotal,
		ProcsCreated: miscStat.ProcsCreated,
		ProcsRunning: miscStat.ProcsRunning,
		ProcsBlocked: miscStat.ProcsBlocked,
		Ctxt:         miscStat.Ctxt,
	}

	return info
}
